<template>
	<view class="login">
		<view class="container">
			<!--插画-->
			<view class="right-top-sign"></view>
			<view class="wrapper">
				<view class="left-top-sign">LOGIN</view>
				<view class="welcome">
					欢迎回来！
				</view>
				<form @submit="formSubmit" @reset="formReset">
					<view class="input-content">
						<view class="input-item">
							<text class="tit">用户名</text>
							<input type="text" name="email" placeholder="请输入用户名" />
						</view>
						<view class="input-item">
							<text class="tit">密码</text>
							<input type="password" name="password" placeholder="请输入密码" maxlength="20" />
						</view>
						<button formType="submit" class="confirm-btn">
							登录
						</button>
					</view>
				</form>
			</view>
		</view>
	</view>
	</view>
</template>
<script>
	import graceChecker from '../../common/graceChecker.js'
	export default {
		name: 'Login',
		data() {
			return {
				rules: [{
						name: "email",
						checkType: "string",
						checkRule: "2,",
						errorMsg: "用户名至少2个字符"
					},
					{
						name: 'password',
						checkType: "string",
						checkRule: "3,12",
						errorMsg: "密码应为6-12个字符"
					}
				]
			};
		},
		methods: {
			async formSubmit(e) {
				const formData = e.detail.value;
				// 进行表单检查
				const checkRes = graceChecker.check(formData, this.rules);
				if (!checkRes) {
					uni.showToast({
						title: graceChecker.error,
						icon: "none"
					});
					return
				}
				// 登录接口
				const res = await this.$u.api.login(formData)
				// 缓存token
				this.$u.vuex('vuex_token',res.access_token)
				// 请求用户信息
				const userInfo = await this.$u.api.userInfo()
				// 缓存用户信息
				this.$u.vuex('vuex_user',userInfo)
			}
		}
	}
</script>
<style lang="scss">
	page {
		background: $color-white;
	}

	.container {
		padding-top: 115px;
		position: relative;
		width: 100vw;
		overflow: hidden;
		background: #fff;

		.wrapper {
			position: relative;
			z-index: 90;
			background: #fff;
			padding-bottom: 40rpx;
		}

		.left-top-sign {
			font-size: 120rpx;
			color: $page-color-base;
			position: relative;
			left: -16rpx;
		}

		.right-top-sign {
			position: absolute;
			top: 80rpx;
			right: -30rpx;
			z-index: 95;

			&:before,
			&:after {
				display: block;
				content: '';
				width: 400rpx;
				height: 80rpx;
				background: #b4f3e2;
			}

			&:before {
				transform: rotate(50deg);
				border-radius: 0 50px 0 0;
			}

			&:after {
				position: absolute;
				right: -198rpx;
				top: 0;
				transform: rotate(-50deg);
				border-radius: 50px 0 0 0;
			}
		}

		.welcome {
			position: relative;
			left: 50rpx;
			top: -90rpx;
			font-size: 46rpx;
			color: #555;
			text-shadow: 1px 0px 1px rgba(0, 0, 0, 0.3);
		}

		.input-content {
			padding: 0 60rpx;
		}

		.input-item {
			display: flex;
			flex-direction: column;
			align-items: flex-start;
			justify-content: center;
			padding: 0 30rpx;
			background: $page-color-light;
			height: 120rpx;
			border-radius: 4px;
			margin-bottom: 50rpx;

			&:last-child {
				margin-bottom: 0;
			}

			.tit {
				height: 50rpx;
				line-height: 56rpx;
				font-size: $font-sm + 2rpx;
				color: $font-color-base;
			}

			input {
				height: 60rpx;
				font-size: $font-base + 2rpx;
				color: $font-color-dark;
				width: 100%;
			}
		}

		.confirm-btn {
			color: $color-white;
			background-color: $base-color;

			&.button-hover {
				opacity: 0.8;
			}
		}
	}
</style>
